package com.example.administrator.androidros.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.androidros.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by huhaichao on 2018/3/14.
 */

public class ApplyDialog extends Dialog {
    @BindView(R.id.view_apply_dialog_img_close)
    protected ImageView view_apply_dialog_img_close;
    @BindView(R.id.view_apply_dialog_tv_appliance_member_id)
    protected TextView view_apply_dialog_tv_appliance_member_id;
    @BindView(R.id.view_apply_dialog_tv_appliance_id)
    protected TextView view_apply_dialog_tv_appliance_id;
    @BindView(R.id.view_apply_dialog_tv_appliance_type)
    protected TextView view_apply_dialog_tv_appliance_type;
    @BindView(R.id.view_apply_dialog_bt_apply)
    protected Button view_apply_dialog_bt_apply;
    private Context mContext;
    private OnApplyClickListener onApplyClickListener;
    private String memberId;
    private String applianceId;
    private String applianceType;

    public ApplyDialog(Context context) {
        super(context, R.style.CustomDialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.view_apply_dialog, null);
        setContentView(view);
        ButterKnife.bind(this);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.8); // 宽度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    public void setMemberId(String memberId) {
        this.memberId = memberId;
    }

    public void setApplianceId(String applianceId) {
        this.applianceId = applianceId;
    }

    public void setApplianceType(String applianceType) {
        this.applianceType = applianceType;
    }

    public void setOnApplyClickListener(OnApplyClickListener onApplyClickListener) {
        this.onApplyClickListener = onApplyClickListener;
    }

    @Override
    public void show() {
        super.show();
        view_apply_dialog_tv_appliance_member_id.setText(memberId);
        view_apply_dialog_tv_appliance_id.setText(applianceId);
        view_apply_dialog_tv_appliance_type.setText(applianceType);
        view_apply_dialog_img_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        view_apply_dialog_bt_apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onApplyClickListener.onApplyClick(view_apply_dialog_tv_appliance_id.getText().toString());
                dismiss();
            }
        });
        setCanceledOnTouchOutside(false);
    }

    public interface OnApplyClickListener {
        void onApplyClick(String applianceId);
    }
}
